package com.fieldchain.ex.fa100.ui.money.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.BillBean;
import com.fieldchain.ex.fa100.ui.money.adapter.BillAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequestTwo;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/22
 */
public class BillActivity extends MyBaseActivity {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private RadioGroup rg;
    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private SmartRefreshLayout refresh;
    private RecyclerView rv;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private BillAdapter adapter;
    private List<BillBean> mList = new ArrayList<>();
    private int page=1;
    private int tag;
    private String id="";
    private String name="";

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        rg = findViewById(R.id.rg);
        rb0 = findViewById(R.id.rb0);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rb3 = findViewById(R.id.rb3);
        refresh = findViewById(R.id.refresh);
        rv = findViewById(R.id.rv);
        reaEmpty = findViewById(R.id.rea_empty);
        tvEmpty = findViewById(R.id.tv_empty);
    }

    @Override
    public void setValue() {
        id = getIntent().getStringExtra("coinId");
        name=getIntent().getStringExtra("coin");
        tvCenter.setText(name.toUpperCase()+" 账单");
        initRv();
        initRb();

        reaBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initRv() {
        adapter = new BillAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                refreshLayout.finishLoadMore(1000);
                getData();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mList.clear();
                refresh.finishRefresh(1000);
                getData();
            }
        });
        getData();
    }

    private void initRb() {
        setTags(rb0, 0);
        setTags(rb1, 12);
        setTags(rb2, 13);
        setTags(rb3, 1);
    }

    public void setTags(RadioButton bt, final int pos) {
        bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    page=1;
                    mList.clear();
                    tag = pos;
                    getData();
                }
            }
        });
    }

    private void getData() {
        HashMap map = new HashMap();
        map.put("page", page + "");
        map.put("coinId", id);
        map.put("coinName",name.toLowerCase());
        if(tag>0)
        map.put("operation", tag + "");
        HttpRequest.getInstance().getRequest(map, Constant.transaction, new ListRequestTwo<BillBean>() {
            @Override
            public void onSucess(List<BillBean> list, int code, String msg) {
                if (code == 200) {
                    mList.addAll(list);
                    adapter.setData(mList);
                }

                if (list.size() == 0) {
                    if (page > 1) {
                        page--;
                    }
                }

                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFaild(Exception e) {
                if (page > 1) {
                    page--;
                }

                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_bill;
    }
}
